﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Calibration.CApCalibrationSetsXmlManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands.Calibration;

internal static class CApCalibrationSetsXmlManager
{
  internal const string CALIBRATION = "calibration";
  internal const string CALIBRATION_SETS = "calibrationSets";
  internal const string CALIBRATION_SET = "calibrationSet";
  internal const string ID_ATTR = "ID";
  internal const string NAME = "name";
  internal const string GLOBAL_ID = "global-id";
  internal const string DEVICE = "device";
  internal const string FRAME = "frame";
  internal const string LOCATION = "location";
  internal const string POS = "pos";
  internal const string X = "x";
  internal const string Y = "y";
  internal const string Z = "z";
  internal const string RX = "rx";
  internal const string RY = "ry";
  internal const string RZ = "rz";
  internal const string DESCRIPTION = "description";
  internal const string LAST_MODIFIED = "lastModified";
  internal const string IS_MAINTAIN_ORIENTATION = "isMaintainOrientation";
  internal const string IS_APPLIED = "isApplied";
  internal const string APPLIED_ORDER = "appliedOrder";
  internal const string CALIBRATION_PAIRS = "calibrationPairs";
  internal const string CALIBRATION_PAIR = "calibrationPair";
  internal const string VIRTUAL_POINT = "virtualPoint";
  internal const string ID_ELEM = "id";
  internal const string ACTUAL_POINT = "actualPoint";
  internal const string IS_ACTIVE = "isActive";
  internal const string TARGET_OBJECTS = "targetObjects";
  internal const string TARGET_OBJECT = "targetObject";
  internal const string TRANSFORMATION = "transformation";
  internal const string TRANSFORMATION_EXCEPTION = "transformationException";
  internal const string TRANSLATIONONLY = "translationOnly";
  internal const string CALIBRATION_XML_FILE_NAME = "calibration.xml";

  internal static void SaveCalibrationXml(
    ObservableCollection<CApCalibrationSet> calibrationSets)
  {
    TxCalibrationEx.CalibrationSetsData = CApCalibrationSetsXmlManager.GenerateCalibrationXml(calibrationSets);
  }

  private static string GenerateCalibrationXml(
    ObservableCollection<CApCalibrationSet> calibrationSets)
  {
    XmlDocument xmlDocument = new XmlDocument();
    XmlElement element1 = xmlDocument.CreateElement("calibration");
    xmlDocument.AppendChild((XmlNode) element1);
    XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", (string) null, (string) null);
    XmlElement documentElement = xmlDocument.DocumentElement;
    xmlDocument.InsertBefore((XmlNode) xmlDeclaration, (XmlNode) documentElement);
    XmlElement element2 = xmlDocument.CreateElement(nameof (calibrationSets));
    element1.AppendChild((XmlNode) element2);
    foreach (CApCalibrationSet calibrationSet in (Collection<CApCalibrationSet>) calibrationSets)
    {
      XmlNode node = calibrationSet.ToXml().SelectSingleNode("/calibrationSet");
      XmlNode newChild = xmlDocument.ImportNode(node, true);
      element2.AppendChild(newChild);
    }
    StringWriter w1 = new StringWriter();
    XmlTextWriter w2 = new XmlTextWriter((TextWriter) w1);
    xmlDocument.WriteTo((XmlWriter) w2);
    return w1.ToString();
  }

  internal static bool LoadCalibrationXml(
    ref ObservableCollection<CApCalibrationSet> calibrationSets)
  {
    bool flag = true;
    string calibrationSetsData = TxCalibrationEx.CalibrationSetsData;
    if (!calibrationSetsData.Equals(""))
      flag = CApCalibrationSetsXmlManager.ParseCalibrationXml(calibrationSetsData, ref calibrationSets);
    return flag;
  }

  private static bool ParseCalibrationXml(
    string calibrationData,
    ref ObservableCollection<CApCalibrationSet> calibrationSets)
  {
    bool calibrationXml = false;
    if (calibrationData != null && calibrationData != "")
    {
      XmlDocument xmlDocument = new XmlDocument();
      try
      {
        xmlDocument.LoadXml(calibrationData);
        if (!xmlDocument.SelectSingleNode("//calibrationSets").HasChildNodes)
          return true;
        XmlNodeList xmlNodeList = xmlDocument.SelectNodes("//calibrationSets//calibrationSet");
        if (xmlNodeList.Count > 0)
        {
          calibrationSets = new ObservableCollection<CApCalibrationSet>();
          int num = 0;
          foreach (XmlNode node in xmlNodeList)
          {
            CApCalibrationSet newSet = (CApCalibrationSet) null;
            bool fromXml = CApCalibrationSet.CreateFromXml(node, ref newSet);
            calibrationSets.Add(newSet);
            if (fromXml)
              ++num;
          }
          if (num == xmlNodeList.Count)
            calibrationXml = true;
        }
      }
      catch (XmlException ex)
      {
      }
    }
    return calibrationXml;
  }

  internal static XmlElement AddElement(XmlDocument xDoc, XmlElement parent, string elementName)
  {
    XmlElement element = xDoc.CreateElement(elementName);
    parent.AppendChild((XmlNode) element);
    return element;
  }

  internal static void SetAttributeToElement(
    XmlDocument xDoc,
    XmlElement element,
    string attributeName,
    string attributeValue)
  {
    XmlAttribute attribute = xDoc.CreateAttribute(attributeName);
    attribute.Value = attributeValue;
    element.Attributes.Append(attribute);
  }

  internal static XmlNode GetXMLNodeByName(XmlNode parentNode, string name)
  {
    XmlNode xmlNodeByName = (XmlNode) null;
    if (parentNode != null)
      xmlNodeByName = parentNode.SelectSingleNode("./" + name);
    return xmlNodeByName;
  }

  internal static string GetXmlNodeText(XmlNode node)
  {
    string xmlNodeText = "";
    if (node != null)
      xmlNodeText = node.InnerText;
    return xmlNodeText;
  }

  internal static string GetXmlNodeAttribute(XmlNode node, string attributeName)
  {
    string xmlNodeAttribute = "";
    if (node != null && node.Attributes != null && node.Attributes.Count > 0 && node.Attributes[attributeName] != null)
      xmlNodeAttribute = node.Attributes[attributeName].Value;
    return xmlNodeAttribute;
  }

  internal static bool GetDoubleValue(XmlNode parentNode, string nodeName, ref double doubleValue)
  {
    bool doubleValue1 = false;
    XmlNode node = parentNode.SelectSingleNode("./" + nodeName);
    if (node != null && double.TryParse(CApCalibrationSetsXmlManager.GetXmlNodeText(node), out doubleValue))
      doubleValue1 = true;
    return doubleValue1;
  }

  internal static bool GetBoolValue(XmlNode parentNode, string nodeName, ref bool boolValue)
  {
    bool boolValue1 = false;
    XmlNode node = parentNode.SelectSingleNode("./" + nodeName);
    if (node != null && bool.TryParse(CApCalibrationSetsXmlManager.GetXmlNodeText(node), out boolValue))
      boolValue1 = true;
    return boolValue1;
  }

  internal static bool GetIntValue(XmlNode parentNode, string nodeName, ref int intValue)
  {
    bool intValue1 = false;
    XmlNode node = parentNode.SelectSingleNode("./" + nodeName);
    if (node != null && int.TryParse(CApCalibrationSetsXmlManager.GetXmlNodeText(node), out intValue))
      intValue1 = true;
    return intValue1;
  }

  internal static bool GetPointIdAndPos(
    XmlNode parentNode,
    string pointName,
    ref string pointId,
    ref string pointLocName,
    ref TxTransformation pointLocation)
  {
    bool flag1 = false;
    XmlNode node1 = parentNode.SelectSingleNode("./" + pointName);
    if (node1 != null)
    {
      pointId = CApCalibrationSetsXmlManager.GetXmlNodeAttribute(node1, "global-id");
      XmlNode node2 = parentNode.SelectSingleNode($"./{pointName}/location/name");
      if (node2 != null)
        pointLocName = CApCalibrationSetsXmlManager.GetXmlNodeText(node2);
      flag1 = true;
    }
    string pointLocPosX = "";
    string pointLocPosY = "";
    string pointLocPosZ = "";
    string pointLocRotX = "";
    string pointLocRotY = "";
    string pointLocRotZ = "";
    bool flag2 = flag1 && CApCalibrationSetsXmlManager.LoadPointLocPos(parentNode, pointName, ref pointLocPosX, ref pointLocPosY, ref pointLocPosZ) && CApCalibrationSetsXmlManager.LoadPointLocRot(parentNode, pointName, ref pointLocRotX, ref pointLocRotY, ref pointLocRotZ);
    double result1 = 0.0;
    double result2 = 0.0;
    double result3 = 0.0;
    double result4 = 0.0;
    double result5 = 0.0;
    double result6 = 0.0;
    bool pointIdAndPos = flag2 && double.TryParse(pointLocPosX, out result1) && double.TryParse(pointLocPosY, out result2) && double.TryParse(pointLocPosZ, out result3) && double.TryParse(pointLocRotX, out result4) && double.TryParse(pointLocRotY, out result5) && double.TryParse(pointLocRotZ, out result6);
    if (pointIdAndPos)
      pointLocation = new TxTransformation(new TxVector(result1, result2, result3), new TxVector(result4, result5, result6), (TxTransformation.TxRotationType) 1);
    return pointIdAndPos;
  }

  internal static bool LoadPointLocPos(
    XmlNode parentNode,
    string pointName,
    ref string pointLocPosX,
    ref string pointLocPosY,
    ref string pointLocPosZ)
  {
    bool flag = false;
    XmlNode node = parentNode.SelectSingleNode($"./{pointName}/location/pos");
    if (node != null)
    {
      pointLocPosX = CApCalibrationSetsXmlManager.GetXmlNodeAttribute(node, "x");
      pointLocPosY = CApCalibrationSetsXmlManager.GetXmlNodeAttribute(node, "y");
      pointLocPosZ = CApCalibrationSetsXmlManager.GetXmlNodeAttribute(node, "z");
      flag = true;
    }
    return flag;
  }

  internal static bool LoadPointLocRot(
    XmlNode parentNode,
    string pointName,
    ref string pointLocRotX,
    ref string pointLocRotY,
    ref string pointLocRotZ)
  {
    bool flag = false;
    XmlNode node = parentNode.SelectSingleNode($"./{pointName}/location/pos");
    if (node != null)
    {
      pointLocRotX = CApCalibrationSetsXmlManager.GetXmlNodeAttribute(node, "rx");
      pointLocRotY = CApCalibrationSetsXmlManager.GetXmlNodeAttribute(node, "ry");
      pointLocRotZ = CApCalibrationSetsXmlManager.GetXmlNodeAttribute(node, "rz");
      flag = true;
    }
    return flag;
  }

  internal static ITxObject GetObject(string id)
  {
    ITxObject itxObject = (ITxObject) null;
    if (id != null && id.Length > 0)
    {
      itxObject = TxEngineeringDataInternal.GetObjectFromIdString(id);
      if (itxObject == null && TxTcUtilities.IsTcAnyPlatform)
      {
        string str = TxTcUtilities.RestoreSceneDetailsMissingOccurrence(id);
        if (str != null)
          itxObject = TxEngineeringDataInternal.GetObjectFromIdString(str);
      }
    }
    return itxObject;
  }

  internal static string GetID(ITxObject obj)
  {
    string id = (string) null;
    if (obj != null)
      id = TxEngineeringDataInternal.GetIdStringFromObject(obj);
    return id;
  }

  public static string GetCalibrationXmlFilePath(string nodeExtId)
  {
    return CApCalibrationSetsXmlManager.GetDocumentByRole(nodeExtId, (TxFileRoleEnum) 2) + "\\calibration.xml";
  }

  public static string GetEngineeringDataFolderPath(string nodeExtId)
  {
    return CApCalibrationSetsXmlManager.GetDocumentByRole(nodeExtId, (TxFileRoleEnum) 2);
  }

  private static string GetDocumentByRole(string nodeExtId, TxFileRoleEnum role)
  {
    return new TxDocumentEx().NodeGetAttachmentFullPath(new TxProcessModelId(nodeExtId), role);
  }
}
